using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using WeatherMcpClient.Services;
using WeatherMcpClient.Views;

namespace WeatherMcpClient.Controllers
{
    public class WeatherController
    {
        private readonly IWeatherService _service;
        private readonly IWeatherView _view;

        public WeatherController(IWeatherService service, IWeatherView view)
        {
            _service = service;
            _view = view;
        }

        public async Task RunAsync()
        {
            _view.ShowTitle();
            _view.ShowInfo("正在连接到服务器...");

            if (!await _service.CheckHealthAsync())
            {
                _view.ShowInfo("❌ 无法连接到服务器\n请确保服务端正在运行: dotnet run --urls=http://localhost:5000");
                return;
            }

            _view.ShowInfo("✅ 成功连接到服务器\n");
            _view.ShowCityCoordinates();

            while (true)
            {
                var choice = _view.ReadMainMenuChoice();
                if (choice == "4") break;

                switch (choice)
                {
                    case "1":
                        await HandleCurrentAsync();
                        break;
                    case "2":
                        await HandleForecastAsync();
                        break;
                    case "3":
                        await HandleBatchAsync();
                        break;
                    default:
                        _view.ShowInfo("无效选择，请重新输入");
                        break;
                }
            }

            _view.ShowInfo("感谢使用天气查询工具！");
        }

        private async Task HandleCurrentAsync()
        {
            _view.ShowInfo("🌤️ 当前天气查询\n================");
            var (lat, lon) = _view.ReadCoordinates();
            if (!lat.HasValue || !lon.HasValue) return;
            var text = await _service.GetCurrentWeatherAsync(lat.Value, lon.Value);
            _view.ShowSection("📊 当前天气信息:", text);
        }

        private async Task HandleForecastAsync()
        {
            _view.ShowInfo("📅 3天天气预报查询\n==================");
            var (lat, lon) = _view.ReadCoordinates();
            if (!lat.HasValue || !lon.HasValue) return;
            var text = await _service.GetForecastAsync(lat.Value, lon.Value);
            _view.ShowSection("📊 3天天气预报:", text);
        }

        private async Task HandleBatchAsync()
        {
            _view.ShowInfo("🏙️ 批量城市查询\n================");
            var list = _view.ReadBatchCoordinates();
            if (list.Count == 0)
            {
                _view.ShowInfo("未输入任何城市坐标");
                return;
            }

            _view.ShowInfo($"\n开始查询 {list.Count} 个城市的天气...\n");

            foreach (var (name, lat, lon) in list)
            {
                try
                {
                    _view.ShowInfo($"🌤️ 正在查询 {name} 的天气...");
                    var text = await _service.GetCurrentWeatherAsync(lat, lon);
                    _view.ShowSection($"📍 {name} 天气:", text);
                    await Task.Delay(500);
                }
                catch (Exception ex)
                {
                    _view.ShowInfo($"❌ 查询 {name} 时出错: {ex.Message}");
                }
            }
        }
    }
}

